package com.maretksa.absensi;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverCommandProcessor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import com.google.common.base.Function;
import com.thoughtworks.selenium.DefaultSelenium;

public class MulaiAbsen extends Abstract{

	@Test
	public void testLoginMulai() throws Exception{
		
		/* Use Firefox */
		WebDriver browser = new FirefoxDriver();
		DefaultSelenium selenium = new DefaultSelenium(new WebDriverCommandProcessor("http://www.google.com", browser));
		
		/* Use Chrome Driver */
//		System.setProperty("webdriver.driver.chrome", "/Users/maretska/Documents/chromedriver");
//		WebDriver browser = new ChromeDriver();
//		DefaultSelenium selenium = new DefaultSelenium(new WebDriverCommandProcessor("http://www.google.com", browser));
		
		/* Start Test Case */
		browser.get(URL);
		browser.manage().window().maximize();
		browser.findElement(By.xpath(loginFB)).click();
		waitForText(3);
		
		/* To handle two Window */
		Set<String> handels = browser.getWindowHandles();
		Iterator<String> it = handels.iterator();
		while (it.hasNext()){
			String parent = it.next();
			String newwin = it.next();
			browser.switchTo().window(newwin);
		
			//Invalid Input ID
			waitForElement(selenium, userNameFB);
			WebElement inputUserNameFB = browser.findElement(By.xpath(userNameFB));
			inputUserNameFB.sendKeys("fake");
			waitForElement(selenium, password);
			WebElement inputPasswordFB = browser.findElement(By.xpath(password));
			inputPasswordFB.sendKeys("fake");
			waitForElement(selenium, masuk);
			browser.findElement(By.xpath(masuk)).click();
			assertFalse("Error Display",!selenium.isVisible(Alert));
			System.out.println("Error Display");
			
			//Valid Input ID
			waitForElement(selenium, INPUT_NAME_EMAIL);
			browser.findElement(By.xpath(INPUT_NAME_EMAIL)).sendKeys(HIdden_email);
			waitForElement(selenium, INPUT_NAME_PASS);
			browser.findElement(By.xpath(INPUT_NAME_PASS)).sendKeys(Hidden_Pass);
			waitForElement(selenium, masuk);
			browser.findElement(By.xpath(masuk)).click();
			
			
//			browser.close();
			browser.switchTo().window(parent);
		}
		
		WebElement inputEmployeeID = browser.findElement(By.xpath(employeeID));
//		waitForElement(selenium, "div.avs-alert-text");
//		inputEmployeeID.sendKeys("0000");
//		browser.findElement(By.xpath(continueButton)).click();
//		assertFalse("Login Failed", !selenium.isVisible("div.avs-alert-text"));
//		browser.findElement(By.cssSelector("div.avs-ok-button")).click();
		
		inputEmployeeID.sendKeys("I0061");
		browser.findElement(By.xpath(continueButton)).click();
		waitForText(10);
		waitForElement(selenium, profile);
		browser.findElement(By.xpath(profile)).click();
		waitForElement(selenium, attandanceList);
		browser.findElement(By.xpath(attandanceList)).click();
		waitForElement(selenium, dailyAttandance);
		browser.findElement(By.xpath(dailyAttandance)).click();
		waitForElement(selenium, husein);
		
		/* To scroll page with Actions Function */
		WebElement huseinName = browser.findElement(By.xpath(husein));
		WebElement febrianto = browser.findElement(By.xpath(Febrianto));
		Actions builder = new Actions(browser);
		Action dragAndDrop = builder.clickAndHold(huseinName)
		.moveToElement(febrianto)
		.release(febrianto)
		.build();
		dragAndDrop.perform();
		
		waitForElement(selenium, Febrianto);
		waitForElement(selenium, backButton);
		browser.findElement(By.xpath(backButton)).click();
		testAttandanceList(selenium, browser);
		
	}
	
	@Test
	public void testAttandanceList(DefaultSelenium selenium ,WebDriver browser) throws Exception{
		waitForElement(selenium, ATTENDACE_MENU_BUTTON_LIST);
		browser.findElement(By.xpath(ATTENDACE_MENU_BUTTON_LIST)).click();
		waitForElement(selenium, inputDatePicker);
		browser.findElement(By.xpath(inputDatePicker)).click();
		waitForElement(selenium, iconNextMount);
		for (int a = 1 ;; a++){
			browser.findElement(By.xpath(iconNextMount)).click();
			waitForText(2);
			if (a>=3) break;
			try { if (selenium.isVisible(january)) break;} catch(Exception e){}
		}
		waitForElement(selenium, date31);
		browser.findElement(By.xpath(date31)).click();
		waitForText(2);
		browser.findElement(By.xpath(untilDatePicker)).click();
		waitForText(1);
		browser.findElement(By.xpath(date2)).click();
		waitForText(2);
		browser.findElement(By.xpath(SEARCH_BUTTON_TEXT)).click();
		waitForElement(selenium, searchEmployeeID);
		browser.findElement(By.xpath(searchEmployeeID)).click();
		waitForText(2);
		browser.findElement(By.xpath("//input[@class='attendance-search-input-text']")).sendKeys("I0069");
		waitForText(3);
		browser.findElement(By.xpath("//div[@class='attendance-list-button-search']")).click();
		waitForElement(selenium, IKA_WIJANTI);
		
		/* To scroll page with Javascripts Function */
		JavascriptExecutor jse = (JavascriptExecutor)browser;
		jse.executeScript("scroll(0, 250)");
		browser.findElement(By.xpath(backButton)).click();
	}

}
